<?php
/*
 * 后台菜单控制器
 */
namespace Admin\Controller;
use  Think\Controller;

class MenuController extends CommonController {
	public function add (){//添加菜单
		if($_POST){
			if(!isset($_POST['name']) || !$_POST['name']){
				return show(0, '菜单名不能为空');
			}
			if(!isset($_POST['m']) || !$_POST['m']){
				return show(0, '模块名不能为空');
			}
			if(!isset($_POST['c']) || !$_POST['c']){
				return show(0, '控制器名不能为空');
			}
			if(!isset($_POST['f']) || !$_POST['f']){
				return show(0, '方法名不能为空');
			}
			if($_POST['menu_id']){
				return $this->save($_POST);
			}
			
			
			$menuId = D('Menu')->insert($_POST);
			if($menuId){
				return show(1, '添加成功！',$menuId);
			}
			return show(0, '添加失败',$menuId);
			
		}else{
			$this->display();
		}
	}
	
	public function index (){//
		/**
		 * 分页操作
		 */
		 $data=array();
		 if(isset($_REQUEST['type']) && in_array($_REQUEST['type'], array(0,1))){
		 	$data['type'] = intval($_REQUEST['type']);
			 $this->assign('type',$data['type']);
		 }else{
		 	$this->assign('type',-100);
		 }
		 
		 $page = $_REQUEST['p'] ? $_REQUEST['p']:1;
		 $pageSize = $_REQUEST['pageSize'] ?$_REQUEST['pageSize'] : 5;
		 $menus = D("Menu")->getMenus($data,$page,$pageSize);
		 $menusCount = D("Menu")->getMenusCount($data);
		 
		 $res = new \Think\Page($menusCount,$pageSize);
		 $pageRes = $res->show();
//		 print_r($pageRes);
		 $this->assign('pageRes',$pageRes);
		 $this->assign('menus',$menus);
		$this->display();
		
	}
	
	
	
	public function edit () {
		$menuId = $_GET['id'];
		$menu=D("Menu")->find($menuId);
		$this->assign('menu',$menu);
		$this->display();
	}
	
	public function save($data){
		$menuId = $data['menu_id'];
		unset($data['menu_id']);
		try{
			$id=D("Menu")->updataMenuById($menuId,$data);
			if($id===false){
				return show(0, '更新失败');
			}
			return show(1, '更新成功');
		}catch(Exception $e){
			return show(0, $e->getMessage());
		}
	}
	
	
	public function del (){
		try{
			if($_POST){
				$id=$_POST['id'];
				$status=$_POST['status'];
				$res=D('Menu')->updataStatusById($id,$status);
				if($res){
					return show(1, '操作成功！');
				}else{
					return show(0, '操作失败！');
				}
			}
		}catch(Exception $e){
			return show(0, $e->getMessage());
		}
		return show(0, '没有提交的数据');
	}
	
	public function listorder(){
		$listoder=$_POST['listorder'];
		$jumpUrl=$_SERVER['HTTP_REFERER'];
		$errors=array();
		if($listoder){
			try{
			foreach($listoder as $menuId=>$v){
				$id=D('Menu')->updataMenuListorderById($menuId,$v);
				if($id===false){
					$errors[]=$meunId;
				}
			}
			}catch(Exception $e){
				return show(0, $e->getMessage(),array('jump_url'=>$jumpUrl));
			}
			if($errors){
				return show(0, '排序失败-'.implode(',', $errors),array('jump_url'=>$jumpUrl));
			}
			return show(1, '排序成功',array('jump_url'=>$jumpUrl));
		}
		return show(0, '排序数据失败成功',array('jump_url'=>$jumpUrl));
	}
}
